package org.idea.spring.bean.source;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.idea.spring.bean.beandefinitionbuilder.User;
/**
 * @Author linhao
 * @Date created in 4:44 下午 2021/5/3
 */
public class AddBeanAfterRefreshDemo {



    public static void main(String[] args) {
        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
        annotationConfigApplicationContext.register(AddBeanAfterRefreshDemo.class);
        annotationConfigApplicationContext.refresh();
        try {
            User user0 = annotationConfigApplicationContext.getBean(User.class);
            System.out.println("user0 is "+user0);
        }catch (Exception b){
            b.printStackTrace();
        }
        System.out.println("启动后手动注入bean对象");

        BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(User.class);
        beanDefinitionBuilder.setScope("prototype");
        beanDefinitionBuilder
                .addPropertyValue("id",2)
                .addPropertyValue("name","idea");
        annotationConfigApplicationContext.registerBeanDefinition("user",beanDefinitionBuilder.getBeanDefinition());
        User user1 = annotationConfigApplicationContext.getBean(User.class);
        User user2 = annotationConfigApplicationContext.getBean(User.class);

        BeanDefinition beanDefinition = annotationConfigApplicationContext.getBeanDefinition("user");
        System.out.println(beanDefinition.getScope());
        System.out.println(user1==user2);
        annotationConfigApplicationContext.close();
    }
}
